package com.ychug.front.front;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ychug.framework.business.consts.FrontConst;
import com.ychug.framework.config.FreeMarkerConfig;
import com.ychug.framework.core.advice.AdDirective;
import com.ychug.framework.core.advice.NewsDataListDirective;
import com.ychug.framework.util.TempleteUtile;
import com.ychug.framework.web.base.BaseController;
import com.ychug.service.cms.entity.CmsNewsDataEntity;
import com.ychug.service.cms.entity.CmsNewsEntity;
import com.ychug.service.system.entity.SysConfigEntity;
import com.ychug.service.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页 业务处理
 *
 * @author dds
 */
@Controller
public class Index1Controller extends BaseController {

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    @Autowired
    private AdDirective adDirective;
    @Autowired
    private ISysConfigService iSysConfigService;

    @Autowired
    private NewsDataListDirective newsDataListDirective;

    /**
     * 跳转到登录页面
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response){
        return "modules/sys/login";
    }


    @RequestMapping(value = "/home")
    public ModelAndView index(ModelAndView modelAndView,HttpServletRequest request, HttpServletResponse response) {

        try {
            String Path = FrontConst.getTemplateGlobal_Pc_Path();
            String out_path = FrontConst.getTemplateGlobal_Pc_Out_Path();
            System.out.println(Path);
            System.out.println(out_path);
            TempleteUtile templeteUtile = new TempleteUtile();
            Map data = new HashMap();
            data.put("ad",adDirective);
            data.put("news_list",newsDataListDirective);

            CmsNewsDataEntity cmsNewsEntity = new CmsNewsDataEntity();
            cmsNewsEntity.setTitle("333333333333333333333333333333333");
            cmsNewsEntity.setSourceUrl("http://localhost:8082/front/");
            data.put("newss",cmsNewsEntity);
            File outFile = new File(out_path+"5222.html");
            templeteUtile.process("news/news_list.ftl", data, outFile);
            System.out.println("恭喜，生成成功~~");

        }catch (Exception e){

        }
        return modelAndView;
    }

    @RequestMapping(value = "/list")
    public ModelAndView list( ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response, Model model){
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        request.setAttribute("pageNum", pageNum);
        PageHelper.startPage(pageNum, 5);
        List<SysConfigEntity> sysConfigEntityList = iSysConfigService.selectConfigList(new SysConfigEntity());

        PageInfo<SysConfigEntity> tPageInfo = new PageInfo(sysConfigEntityList);

        model.addAttribute("ad", sysConfigEntityList);
        model.addAttribute("page", tPageInfo);
        modelAndView.setViewName("index222");
        return modelAndView;
    }

}
